package configuration;


import java.io.FileInputStream;
import java.io.File;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * This class offers a centralized point of access to property file
 */


public class Configuration {
    
    //--- Attributes -------------------------------------------------------
    protected static Properties prop = null;
    public static String FileName = "configuration.prop";
    public static String Path = System.getProperty ("user.dir") + "/config";
    protected static String ConfigPath ="";
    
    
    private static File FindFile(String fileName) {
        File f = null;
        
        ConfigPath = Configuration.Path + "/";
        f = new File( ConfigPath + fileName);
        if (f.exists () == false) {
            log.info ("Configuration file not found in: " 
                + f.getAbsolutePath ()
                +"\nTrying again...");
            
            ConfigPath = System.getProperty ("user.dir") + "/../config/";
            f = new File(ConfigPath + fileName);
            if (f.exists () == false) {
                log.info ("Configuration file not found in: " + f.getAbsolutePath ());
            }
        }

        return f;
    }
    
    /** **/
    public static void Create () {
        File f;
        prop = new Properties ();
        
        f = FindFile(FileName);
        try {
            prop.load (new FileInputStream (f) );
        } catch (Exception ex) {
            log.fatal ("Error loading configuration file: " +
                Configuration.FileName +
                " from: " + Configuration.Path );
        }
    }
    
    
    /** **/
    public static Properties GetPDFProperties() {
        File f;
        Properties p = new Properties();
        
        f = FindFile(Configuration.Get ("PDFConfigFile"));

                try {
            p.load (new FileInputStream (f) );
        } catch (Exception ex) {
            log.fatal ("Error loading configuration file for PDF: " +
                Configuration.Get ("PDFConfigFile") +
                " from: " + Configuration.Path );
            return null;
        }

        return p;
    }
    
    /** **/
    public static String Get (String propName) {
        if (prop != null) {
            return (String) prop.get (propName);
        }
        log.warn ("Property file not initiallized (asking for:"+propName+".");
        return "";
    }
    
    
    /** **/
    public static String Get (String propName, String defaultProp) {
        String prop = Configuration.Get (propName);
        if (prop == null) {
            log.warn ("Property null in Get(String, String): " + propName);
            return defaultProp;
        }
        if ( prop.compareTo ("") == 0) {
            log.info ("Returning default property:"+defaultProp);
            return defaultProp;
        }
        return prop;
    }
    
    
    /** Adds the path to the result **/
    public static String GetFileName (String prop) {
        String s = Configuration.Get (prop);
        return Path + "/" + s;
    }
    
    /** **/
    public static java.io.File GetFile (String prop) {
        java.io.File f = new java.io.File (Configuration.GetFileName (prop));
        if (f.exists () == false)
            log.fatal ("Error: file not found: " + f);
        return f;
    }
    
    /**
     * @param propName
     * @return property value as an int.
     */
    public static int GetInt (String propName) {
        String prop = Configuration.Get (propName);
        return Integer.parseInt (prop);
    }
    
    /**
     * @param propName
     * @return property value as an int, or default if
     * property not found.
     */
    public static int GetInt (String propName, int defaultProp) {
        String prop = Configuration.Get (propName);
        if ( prop.compareTo ("") == 0)
            return defaultProp;
        return Integer.parseInt (prop);
    }
    
    
    /**  Gets the path of the application
     */
    public static String GetPath () {
        return Path;
    }
    
    public static String GetConfigPath() {
        return ConfigPath;
    }
    
    //--- Log -------------------------------------------------------
    protected static Logger log;
    static {   	log = Logger.getLogger (Configuration.class); }
    
}
